package Servlets;

import java.io.*;
import java.util.*;

import javax.servlet.http.*;

import Corpus.Corpus;
import HTMLCrators.*;
import Main.Consts;
import Main.GeneralFunctions;
import Main.Pair;
import Main.String2;

public class SearchServlet extends SuperServlet{

	private static final long serialVersionUID = 1L;
	private Corpus corpus;

	public SearchServlet(Corpus corpus){
		this.corpus=corpus;
	}

	private String fixQuery(String query){
		System.out.println(query);
		Vector<String> parts=String2.split(query, "*:* && ");
		String ans="";
		for(int i=0;i<parts.size();i++)
			ans+=parts.get(i);

		parts=String2.split(ans, " && *:*");
		ans="";
		for(int i=0;i<parts.size();i++)
			ans+=parts.get(i);

		return ans;
	}

	protected void buildResponse(HttpServletRequest request,HttpServletResponse response) throws IOException {

		LinkedList<Pair<String, String>> pairs=new LinkedList<Pair<String,String>>();


		if (haveField(request, "wheel") && request.getParameter("wheel").equals("yes")){
			pairs.add(new Pair<String, String>("[wheel/tree]", "2"));
		}
		else{
			pairs.add(new Pair<String, String>("[wheel/tree]", "1"));
		}

		if (haveField(request, "query")){
			pairs.add(new Pair<String, String>("[Query]", fixQuery(GeneralFunctions.convertPlus(request.getParameter("query")))));//TODO
		}
		else{
			pairs.add(new Pair<String, String>("[Query]", "*:*"));
		}

		if (request.getParameter("topic")==null)
			pairs.add(new Pair<String, String>("[TopicId]", -1+""));
		else{
			//			System.out.println("SearchServlet 3 "+request.getParameter("topic"));//TODO
			pairs.add(new Pair<String, String>("[TopicId]", request.getParameter("topic")));
		}

		pairs.add(new Pair<String, String>("[Page]", request.getParameter("page")));
		pairs.add(new Pair<String, String>("[NumberInWheel]", Consts.NUMBER_IN_WHEEL+""));
		pairs.add(new Pair<String, String>("[NumberOfTopicsToShow]", Consts.NUMBER_OF_TOPICS_TO_SHOW+""));
		pairs.add(new Pair<String, String>("[URL]", Consts.SERVER_URI));		
		pairs.add(new Pair<String, String>("[CountInPage]", Consts.COUNT_IN_PAGE+""));

		String metadataName="";
		for(int i=0;i<corpus.getAllMetadataNames().size();i++)
			metadataName+=corpus.getAllMetadataNames().get(i).getFirst()+",";
		if (corpus.getAllMetadataNames().size()!=0){
			metadataName=metadataName.substring(0, metadataName.length()-1);		
			pairs.add(new Pair<String, String>("[Metadata1]", "metadata=\""+metadataName+"\".split(\",\");"));
		}
		else
			pairs.add(new Pair<String, String>("[Metadata1]", "metadata=new Array();"));

		String metadataRealName="";
		for(int i=0;i<corpus.getAllMetadataNames().size();i++)
			metadataRealName+=corpus.getAllMetadataNames().get(i).getSecend()+",";
		if (corpus.getAllMetadataNames().size()!=0){
			metadataRealName=metadataRealName.substring(0, metadataRealName.length()-1);
			pairs.add(new Pair<String, String>("[Metadata2]", "metadata2=\""+metadataRealName+"\".split(\",\");"));
		}
		else
			pairs.add(new Pair<String, String>("[Metadata2]", "metadata2=new Array();"));

		//String lang=corpus.getLanguage()==Language.English?"E":"H";

		HTMLCreatorFromFileAndPairs creator=new HTMLCreatorFromFileAndPairs("html\\showResults"+"E"+".html", pairs);
		writeResponse(response, creator);

	}

}